package com.mark.web.data.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mark.common.ServerResponse;
import com.mark.web.data.mapper.ShippingMapper;
import com.mark.web.data.exception.ShippingException;
import com.mark.web.data.entity.po.Shipping;
import com.mark.web.data.service.IShippingService;
import com.mark.web.data.entity.vo.ShippingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 地址模块业务逻辑实现
 * @Author: Mark
 * @CreateDate: 22018/8/14.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Service("shippingServiceImpl")
public class ShippingServiceImpl implements IShippingService{
    @Autowired
    private ShippingMapper shippingMapper;


    //1.用户添加收货地址的业务逻辑
    @Override
    public ServerResponse addShipping(Integer currentUserId, Shipping shipping) {
        try {
            shipping.setUserId(currentUserId);
            int resultCount = shippingMapper.insert(shipping);
            if (resultCount > 0) {
                Map<String, Object> resultMap = Maps.newHashMap();
                resultMap.put("shippingId", shipping.getId());
                return ServerResponse.createBySuccess("添加地址成功", resultMap);
            } else {
                return ServerResponse.createByErrorMessage("添加地址失败");
            }
        }catch (Exception e){
            throw new ShippingException("addShipping",e.getClass()+e.getMessage());
        }
    }

    //2.用户删除收货的地址的业务逻辑（为了防止横向越权 删除的依据是userId+shippingId）
    @Override
    public ServerResponse deleteShipping(Integer currentUserId, Integer shippingId) {
        try {
            int resultCount = shippingMapper.deleteShippingByUserIdAndShippingId(currentUserId, shippingId);
            if (resultCount > 0) {
                return ServerResponse.createBySuccessMessage("删除地址成功");
            } else {
                return ServerResponse.createByErrorMessage("删除地址失败");
            }
        }catch (Exception e){
            throw new ShippingException("deleteShipping",e.getClass()+e.getMessage());
        }
    }

    @Override
    public ServerResponse updateShipping(Integer currentUserId, Shipping shipping) {
        try {
            //用户的ID要设置为当前的用户id
            shipping.setUserId(currentUserId);
            int resultCount = shippingMapper.updateByShipping(shipping);
            if (resultCount > 0) {
                return ServerResponse.createBySuccessMessage("更新地址成功");
            } else {
                return ServerResponse.createByErrorMessage("更新地址失败");
            }
        }catch (Exception e){
            throw new ShippingException("updateShipping",e.getClass()+e.getMessage());
        }
    }

    //4.用户查看他的一项收货地址
    @Override
    public ServerResponse selectShipping(Integer currentUserId, Integer shippingId) {
        try {
            Shipping shipping = shippingMapper.selectByShippingIdAndUserId(currentUserId, shippingId);
            if (shipping == null) {
                return ServerResponse.createByErrorMessage("无法查询到该地址");
            } else {
                return ServerResponse.createBySuccess("查找到的用户收货地址", shipping);
            }
        }catch (Exception e){
            throw new ShippingException("selectShipping",e.getClass()+e.getMessage());
        }
    }

    /**
     * 5.分页查看所有的收货地址
     * @param currentUserId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ServerResponse listShipping(Integer currentUserId, int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Shipping> shippingList = shippingMapper.selectAllByUserId(currentUserId);
            List<ShippingVo> shippingVoList = Lists.newArrayList();
            for (Shipping shippingItem : shippingList) {
                ShippingVo shippingVoItem = assembleShippingVo(shippingItem);
                shippingVoList.add(shippingVoItem);
            }
            PageInfo pageInfo = new PageInfo(shippingVoList);
            return ServerResponse.createBySuccess(pageInfo);
        }catch (Exception e){
            throw new ShippingException("listShipping",e.getClass()+e.getMessage());
        }
    }

    private ShippingVo assembleShippingVo(Shipping shipping) {
        ShippingVo shippingVo = new ShippingVo();
        BeanUtils.copyProperties(shipping,shippingVo);
        String phone = shipping.getReceiverPhone();
        int phoneLength = phone.length();
        phone = phone.substring(0,3)+"...."+phone.substring(phoneLength-4,phoneLength);
        shippingVo.setReceiverPhone(phone);
        return shippingVo;
    }
}
